package com.dolphin.core.exception;

/**
 * @author shida
 */
public class BaseException extends RuntimeException{

    private String responseCode;
    private String responseMsg;
    private Object data;

    protected BaseException(String code, String msg, Object data){
        super(msg);

        this.responseCode = code;
        this.responseMsg = msg;
        this.data = data;
    }

    protected BaseException(String code, String msg, Object data, Throwable e){
        super(msg, e);
        this.responseCode = code;
        this.responseMsg = msg;
        this.data = data;
    }

    public static BaseException build(){
        return build(BaseResponse.FAIL.getCode(), BaseResponse.FAIL.getMsg(), null, null);
    }

    public static BaseException build(Throwable e){
        return build(BaseResponse.FAIL.getCode(), BaseResponse.FAIL.getMsg(), null, e);
    }

    public static BaseException build(String msg){
        return build(BaseResponse.FAIL.getCode(), msg, null, null);
    }

    public static BaseException build(BaseResponse baseResponse){
        return build(baseResponse.getCode(), baseResponse.getMsg(), null,null);
    }

    public static BaseException build(BaseResponse baseResponse, Throwable e){
        return build(baseResponse.getCode(), baseResponse.getMsg(), null, e);
    }

    public static BaseException buildNoException(BaseResponse baseResponse, Object data){
        return new BaseException(baseResponse.getCode(), baseResponse.getMsg(), data, null);
    }

    public static BaseException build(String code, String msg, Object data){
        return new BaseException(code, getMsg(msg, null), data, null);
    }

    public static BaseException build(String code, String msg, Object data, Throwable e){
        return new BaseException(code, getMsg(msg, e), data, e);
    }

    private static String getMsg(String msg, Object data){
        String finalStr = "";
        if(data != null && data instanceof String){
            finalStr = msg + ":" + data.toString();
        }else {
            finalStr = msg;
        }

        return finalStr;
    }

    public String getResponseCode() {
        return responseCode;
    }

    public String getResponseMsg() {
        return responseMsg;
    }

    public Object getData() {
        return data;
    }
}
